package com.ml.loan.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.ml.loan.sys.model.SysConfig;
import com.ml.loan.sys.service.SysConfigService;

import java.util.List;

/**
 * 系统配置
 *
 * @author Mickey
 * @date $ $
 **/
public class ConfigUtils {

    /**
     * 静态内部类，延迟加载，懒汉式，线程安全的单例模式
     */
    private static final class Static {
        private static SysConfigService configService = SpringUtil.getBean(SysConfigService.class);
    }

    /**
     * 根据配置key获取主应用下的配置
     *
     * @param key
     * @return
     */
    public static JSONObject getJSONConfig(String key) {
        return getConfigObj(key, JSONObject.class);
    }

    public static SysConfig getConfig(String key) {
        return Static.configService.getSysConfig(key);
    }

    public static <T> T getConfigObj(String key, Class<T> clazz) {
        return Static.configService.getConfigObj(key, clazz);
    }

    public static <T> List<T> getConfigList(String key, Class<T> clazz) {
        return Static.configService.getConfigList(key, clazz);
    }

    /**
     * 系统参数
     *
     * @param key
     * @return
     */
    public static String getSystemParam(String key) {
        SysConfig system = getConfig(key);
        return system != null ? system.getConfValue() : null;
    }

}
